package com.www.ve.utils;

import com.www.ve.exception.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一响应数据
 *
 * @param <T>
 */
@Data
@ApiModel(value = "响应")
public class Result<T> {
    @ApiModelProperty(value = "编码 0表示成功，其他值表示失败")
    private int code = 0;

    @ApiModelProperty(value = "消息内容")
    private String message = "success";

    @ApiModelProperty(value = "响应数据")
    private T data;

    public static <T> Result<T> ok() {
        return ok(null);
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> ok(ErrorCode code, String msg) {
        Result<T> result = new Result<>();
        result.setMessage(msg);
        result.setCode(code.getCode());
        return result;
    }

    public static <T> Result<T> error() {
        return error(ErrorCode.INTERNAL_SERVER_ERROR);
    }

    public static <T> Result<T> error(String msg) {
        return error(ErrorCode.INTERNAL_SERVER_ERROR.getCode(), msg);
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
}
